import {showModal} from "@tarojs/taro";

class Modal {
    constructor() {
    }

    alert(content: string, params?: IModalParams) {
        params = params || {};
        showModal({
            content,
            title: params.title ?? '提示',
            showCancel: false,
            success: (res) => {
                if (typeof params?.callback === "function") params.callback(res);
            }
        })
            .then(() => {
            })
    }

    confirm(content: string, params?: IModalConfirmParams) {
        params = params || {};
        showModal({
            content,
            title: params.title ?? '提示',
            showCancel: typeof params.showCancel === 'boolean' ? params.showCancel : true,
            confirmText: params.confirmText ?? '确认',
            confirmColor: params.confirmColor ?? '#666',
            cancelText: params.cancelText ?? '取消',
            cancelColor: params.cancelColor ?? "#808080",
            success: (res) => {
                if (typeof params?.callback === "function") params.callback(res);
            }
        })
            .then(() => {
            })
    }

    error(content: string, params?: IModalParams) {
        showModal({
            title: '错误',
            content,
            showCancel: false,
            success: (res) => {
                if (typeof params?.callback === "function") params.callback(res);
            }
        })
            .then(() => {
            })
    }
}

export default new Modal()


type IModalParams = {
    title?: string
    callback?: (res: { confirm: boolean, cancel: boolean }) => void
}

type IModalConfirmParams = IModalParams & {
    //  是否显示取消按钮
    showCancel?: boolean
    //  取消按钮的文字，最多 4 个字符
    cancelText?: string
    //  取消按钮的文字颜色，必须是 16 进制格式的颜色字符串
    cancelColor?: string
    //  确认按钮的文字，最多 4 个字符
    confirmText?: string
    //  确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
    confirmColor?: string
}
